package com.wanhe.viewconf.views.child;

import android.content.Context;
import android.graphics.Canvas;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.wanhe.base.model.disgn.Event;
import com.wanhe.base.model.disgn.ViewEvent;
import com.wanhe.base.model.disgn.ViewItem;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.shoppingcart.manager.CartManager;
import com.wanhe.viewconf.views.child.interfaces.IBindingViewFunc;
import com.wanhe.viewconf.views.child.interfaces.ICounterViewFunc;
import com.wanhe.viewconf.views.child.interfaces.IViewReady;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by cdj on 2017/7/26.
 */

public class MealBookGroupView extends MealBookChildItemView
		implements
			IViewReady ,
			IBindingViewFunc ,
			ICounterViewFunc {

	private Map<String, MealBookChildItemView> groupChilds = new HashMap<>();

	public MealBookGroupView(Context context) {
		super(context);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		callSelfEvent("ready");
	}

	@Override
	public void ready(Event event, ViewEvent.Coor coor) {

	}

	@Override
	public void bind(Event event, ViewEvent.Coor coor) {

		/**
		 * 早期旧的逻辑，通过其他控件执行绑定的时候，将菜品ID在这里注册后期购物车变动的时候回调产生相应的回调
		 * 
		 * @author 陈德基
		 * @time 2017/7/29 17:44
		 */
		if (ObjectUtil.checkObject(event)) {
			String dishID = event.getArgs().getDishID();
			if (!TextUtils.isEmpty(dishID)) {
				groupChilds.put(dishID, (MealBookChildItemView) event.getArgs().getCusObj());
			}
		}

		/**
		 * 绑定的话需要将数据回传给请求绑定的控件
		 * 
		 * @author 陈德基
		 * @time 2017/7/29 17:43
		 */
		int fromCid = event.fromCid;
		event.setCid(fromCid);
		event.fromCid = this.mItem.getCid();
		ViewItem.CountRule rule = new ViewItem.CountRule();
		rule.setMax(this.mItem.getMax());
		rule.setMin(this.mItem.getMin());
		rule.setStep(1);
		event.getArgs().setCus(new Gson().toJson(rule));
		sendEvent(event);

	}

	@Override
	public void add(Event event, ViewEvent.Coor coor) {
		if (ObjectUtil.checkObject(event)) {

			String addDishID = event.getArgs().getDishID();
			/**
			 * 将接受来自控件发送的添加事件，但是这里需要先做处理，多出来数量的菜品需要进行删减
			 * 
			 * @author 陈德基
			 * @time 2017/8/2 16:43
			 */

			int count = 0;
			int childCount = 0;
			String reduceDish = null;
			MealBookChildItemView reduceChildItemView = null;
			for (Map.Entry<String, MealBookChildItemView> entry : this.groupChilds.entrySet()) {
				String dishId = entry.getKey();
				childCount = CartManager.getInstance().getSameGuidItemCount(dishId);
				if (childCount > 0) {
					reduceDish = entry.getKey();
					reduceChildItemView = entry.getValue();
				}
				count += childCount;
			}

			/**
			 * 如果添加的是同一道菜，并且数量超多了最多数量，那么不错减操作了
			 * 
			 * @author 陈德基
			 * @time 2017/8/2 19:25
			 */

			if (count >= this.getItem().getMax()) {
				if (ObjectUtil.checkObject(reduceChildItemView) && !addDishID.equals(reduceDish)) {
					reduceChildItemView.callSelfEvent("dec");
				}
			}

			/**
			 * 将多余数量的删除之后，做添加操作
			 * 
			 * @author 陈德基
			 * @time 2017/8/2 17:43
			 */
			// event.setCid(event.fromCid);
			// event.fromCid = this.mItem.getCid();
			// sendEvent(event);

			MealBookChildItemView addItemView = this.groupChilds.get(addDishID);
			addItemView.callSelfEvent("add");
		}
	}

	@Override
	public void dec(Event event, ViewEvent.Coor coor) {

	}
}
